package com.ParkingManager.app.web;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ParkingManager.app.bean.Cash;
import com.ParkingManager.app.bean.Parking;
import com.ParkingManager.app.bean.Record;
import com.ParkingManager.app.service.CashService;
import com.ParkingManager.app.service.ParkingService;
import com.ParkingManager.app.service.RecordService;

@RestController
public class RecordController {
	@Autowired
	private ParkingService parkingService;
	@Autowired
	private CashService cashService;
	@Autowired
	private RecordService recordService;

	/*@RequestMapping("/manage/getTime")
	public Record getTime(int id) {
		return recordService.getTime(id);
	}

	@RequestMapping("/manage/getParkingCoefficient")
	public Parking getParkingCoefficient(int parkingId) {
		return parkingService.getParkingCoefficient(parkingId);
	}*/

	@RequestMapping("/manage/updateIsExist")
	public boolean getCashList(int id) {
		return recordService.updateIsExist(0, id);
	}

	/**
	 * 计算价格
	 * @param id
	 * @return
	 */
	@RequestMapping("/manage/Calculate")
	public double Calculate(int id) {
		double price = 0;
		Record record = recordService.getTime(id);
		Parking parking=parkingService.getParkingCoefficient(record.getParkingId());
		LocalDateTime atime = LocalDateTime.now();
		Timestamp.valueOf(atime);
		Timestamp time = Timestamp.valueOf(atime);
		Timestamp time1 = Timestamp.valueOf(record.getEnterTime());
		Timestamp timetemp=time;
		List<Cash> cash = cashService.getCashList(time);
		for(Cash cashtemp :cash){
			Timestamp cashtime= Timestamp.valueOf(cashtemp.getStartTime());
			if (cashtime.after(time1)) {
				long i=(timetemp.getTime()-cashtime.getTime())/60000;
				price=price+i*(cashtemp.getCharge()/60);
				timetemp=cashtime;
	        }else{
	        	long i=(timetemp.getTime()-time1.getTime())/60000;
	        	price=price+i*(cashtemp.getCharge()/60);
	        	break; //跳出循环
	        }		
		}		
		if(price<3.00)
		{
			price=3.00;
		}
		price=price*parking.getCoefficient();
		if(record.getCarType()==0)
		{
			price=price*parking.getBigCoefficient();
		};
		boolean flag=recordService.updatePrice(price, id);
		return price;
	}
}
